package com.szy.douyou_gateway.web;


import com.szy.douyou_comment.dao.Comment;
import com.szy.douyou_comment.dao.CommentReq;
import com.szy.douyou_gateway.client.CommentClient;
import com.szy.douyou_gateway.config.AccountProfile;
import com.szy.douyou_gateway.config.ResultDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 评论controller
 * @author sunziyan
 * @date 2022/5/12
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentClient commentClient;

    /**
     * 分页查询该文章下所有评论
     * @param articleId
     * @return
     */
    @GetMapping("/queryAllByArticle/{articleId}")
    public ResultDTO queryAllByArticle(@PathVariable("articleId") Long articleId){


        return commentClient.queryAllByArticle(articleId);
    }

    /**
     * 添加一条评论
     * @param comment
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/addOne")
    public ResultDTO addOne(@RequestBody Comment comment){
        return commentClient.addOne(comment);
    }

    /**
     * 分页查询所有评论
     * @param commentReq
     * @return
     */
    @PostMapping("/queryAll")
    public ResultDTO queryAll(@RequestBody CommentReq commentReq){

        return commentClient.queryAll(commentReq);

    }

    /**
     * 删除一条评论
     * @param commentId
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/deleteOne/{commentId}")
    public ResultDTO deleteOne(@PathVariable("commentId")Long commentId){
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (!principal.getUserRight().equals(1)) {
            return ResultDTO.fail("需要管理员权限");
        }
        return commentClient.deleteOne(commentId);
    }
}
